package com.xuecheng.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.ucenter.feignclient.CheckCodeClient;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.mapper.XcUserRoleMapper;
import com.xuecheng.ucenter.model.dto.RegistParamsDto;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.model.po.XcUserRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description 测试controller
 * @date 2022/9/27 17:25
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired
    XcUserMapper userMapper;
    @Autowired
    CheckCodeClient checkCodeClient;
    @Autowired
    XcUserRoleMapper userRoleMapper;

    @RequestMapping("/login-success")
    public String loginSuccess() {

        return "登录成功";
    }


    @RequestMapping("/user/{id}")
    public XcUser getuser(@PathVariable("id") String id) {
        XcUser xcUser = userMapper.selectById(id);
        return xcUser;
    }

    @RequestMapping("/r/r1")
    @PreAuthorize("hasAuthority('p1')")//拥有p1权限方可访问
    public String r1() {
        return "访问r1资源";
    }

    @RequestMapping("/r/r2")
    @PreAuthorize("hasAuthority('p2')")//拥有p2权限方可访问
    public String r2() {
        return "访问r2资源";
    }

//    @PostMapping("/register")
//    public void register(@RequestBody RegistParamsDto registParamsDto) {
////        XcUser xcUser = userMapper.selectById(id);
//        if (registParamsDto.getPassword()!=registParamsDto.getConfirmpwd()) {
//            throw new RuntimeException("密码不一致");
//        }
//        Boolean verify = checkCodeClient.verify(registParamsDto.getCheckcodekey(), registParamsDto.getCheckcode());
//        if(!verify || verify==null){
//            throw new RuntimeException("验证码输入错误");
//        }
//        LambdaQueryWrapper<XcUser> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(XcUser::getUsername,registParamsDto.getUsername());
//        wrapper.eq(XcUser::getPassword,registParamsDto.getPassword());
//        List<XcUser> xcUsers = userMapper.selectList(wrapper);
//        if (xcUsers!=null && xcUsers.size()>0){
//            throw new RuntimeException("用户存在");
//        }
//        XcUser xcUser = new XcUser();
//        BeanUtils.copyProperties(registParamsDto,xcUser);
//        xcUser.setCreateTime(LocalDateTime.now());
//        xcUser.setStatus("1");
//        userMapper.insert(xcUser);
//        XcUserRole userRole = new XcUserRole();
//        userRole.setRoleId("17");
//        userRole.setUserId(xcUser.getId());
//        userRole.setCreateTime(LocalDateTime.now());
//        userRole.setStatus("1");
//        userRoleMapper.insert(userRole);
//    }

}
